<template>
  <div class="profile-container">
    <el-row :gutter="20">
      <!-- 左侧个人信息卡片 -->
      <el-col :span="8">
        <el-card class="profile-card">
          <div class="profile-header">
            <div class="avatar-container">
              <el-avatar :size="100" :src="userInfo.avatar || 'https://cube.elemecdn.com/3/7c/3ea6beec64369c2642b92c6726f1epng.png'" />
            </div>
            <h2 class="username">{{ userInfo.name || '用户名' }}</h2>
            <p class="user-role">{{ userInfo.role || '管理员' }}</p>
          </div>
          
          <el-divider />
          
          <div class="profile-info">
            <div class="info-item">
              <span class="info-label">账号</span>
              <span class="info-value">{{ userInfo.account || 'admin' }}</span>
            </div>
            <div class="info-item">
              <span class="info-label">邮箱</span>
              <span class="info-value">{{ userInfo.email || 'admin@example.com' }}</span>
            </div>
            <div class="info-item">
              <span class="info-label">手机</span>
              <span class="info-value">{{ userInfo.phone || '138****1234' }}</span>
            </div>
            <div class="info-item">
              <span class="info-label">部门</span>
              <span class="info-value">{{ userInfo.department || '技术部' }}</span>
            </div>
            <div class="info-item">
              <span class="info-label">加入时间</span>
              <span class="info-value">{{ userInfo.joinTime || '2025-01-01' }}</span>
            </div>
          </div>
        </el-card>
      </el-col>
      
      <!-- 右侧个人设置 -->
      <el-col :span="16">
        <el-card class="settings-card">
          <template #header>
            <div class="card-header">
              <el-tabs v-model="activeTab">
                <el-tab-pane label="基本资料" name="basic">
                  <template #label>
                    <el-icon><UserFilled /></el-icon>
                    <span>基本资料</span>
                  </template>
                </el-tab-pane>
                <el-tab-pane label="修改密码" name="password">
                  <template #label>
                    <el-icon><Lock /></el-icon>
                    <span>修改密码</span>
                  </template>
                </el-tab-pane>
              </el-tabs>
            </div>
          </template>
          
          <!-- 基本资料表单 -->
          <el-form
            v-if="activeTab === 'basic'"
            ref="basicFormRef"
            :model="basicForm"
            :rules="basicRules"
            label-width="100px"
          >
            <el-form-item label="用户名" prop="name">
              <el-input v-model="basicForm.name" />
            </el-form-item>
            
            <el-form-item label="邮箱" prop="email">
              <el-input v-model="basicForm.email" />
            </el-form-item>
            
            <el-form-item label="手机号" prop="phone">
              <el-input v-model="basicForm.phone" />
            </el-form-item>
            
            <el-form-item label="个人简介" prop="bio">
              <el-input v-model="basicForm.bio" type="textarea" :rows="4" />
            </el-form-item>
            
            <el-form-item label="头像">
              <div class="avatar-upload-container">
                <el-upload
                  class="avatar-uploader"
                  action="#"
                  :show-file-list="false"
                  :auto-upload="false"
                  :on-change="handleAvatarChange"
                  accept="image/jpeg,image/png,image/gif"
                >
                  <div class="avatar-preview">
                    <img v-if="basicForm.avatar" :src="basicForm.avatar" class="avatar" />
                    <div v-else class="avatar-uploader-icon">
                      <el-icon :size="30"><Plus /></el-icon>
                    </div>
                    <div class="avatar-hover-mask">
                      <el-icon :size="20"><EditPen /></el-icon>
                      <span>修改头像</span>
                    </div>
                  </div>
                </el-upload>
                <div class="avatar-tips">
                  <p class="tip-title">上传头像</p>
                  <p class="tip-desc">支持 JPG、PNG、GIF 格式，文件小于 2MB</p>
                </div>
              </div>
            </el-form-item>
            
            <el-form-item>
              <el-button type="primary" @click="submitBasicForm">保存</el-button>
              <el-button @click="resetBasicForm">重置</el-button>
            </el-form-item>
          </el-form>
          
          <!-- 修改密码表单 -->
          <el-form
            v-if="activeTab === 'password'"
            ref="passwordFormRef"
            :model="passwordForm"
            :rules="passwordRules"
            label-width="100px"
          >
            <el-form-item label="当前密码" prop="currentPassword">
              <el-input v-model="passwordForm.currentPassword" type="password" show-password />
            </el-form-item>
            
            <el-form-item label="新密码" prop="newPassword">
              <el-input v-model="passwordForm.newPassword" type="password" show-password />
            </el-form-item>
            
            <el-form-item label="确认新密码" prop="confirmPassword">
              <el-input v-model="passwordForm.confirmPassword" type="password" show-password />
            </el-form-item>
            
            <el-form-item>
              <el-button type="primary" @click="submitPasswordForm">修改密码</el-button>
              <el-button @click="resetPasswordForm">重置</el-button>
            </el-form-item>
          </el-form>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive } from 'vue'
import { ElMessage } from 'element-plus'
import { useAuthStore } from '@/store/auth'

const authStore = useAuthStore()
const activeTab = ref('basic')

// 用户信息
const userInfo = reactive({
  name: authStore.name || '管理员',
  role: '系统管理员',
  account: 'admin',
  email: 'admin@example.com',
  phone: '13800001234',
  department: '技术部',
  joinTime: '2025-01-01',
  avatar: ''
})

// 基本资料表单
const basicFormRef = ref()
const basicForm = reactive({
  name: userInfo.name,
  email: userInfo.email,
  phone: userInfo.phone,
  bio: '这是一段个人简介，请在此处介绍自己。',
  avatar: userInfo.avatar
})

// 基本资料表单验证规则
const basicRules = {
  name: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 2, max: 20, message: '长度在 2 到 20 个字符', trigger: 'blur' }
  ],
  email: [
    { required: true, message: '请输入邮箱', trigger: 'blur' },
    { type: 'email', message: '请输入正确的邮箱地址', trigger: 'blur' }
  ],
  phone: [
    { required: true, message: '请输入手机号', trigger: 'blur' },
    { pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号码', trigger: 'blur' }
  ]
}

// 处理头像变更
const handleAvatarChange = (file: any) => {
  // 文件大小限制为2MB
  const isLt2M = file.size / 1024 / 1024 < 2;
  
  if (!isLt2M) {
    ElMessage.error('上传头像图片大小不能超过 2MB!');
    return;
  }
  
  // 在实际应用中，这里应该上传文件到服务器并获取URL
  // 这里仅做演示，使用本地文件URL
  basicForm.avatar = URL.createObjectURL(file.raw);
  
  // 显示上传成功提示
  ElMessage.success('头像已更新，请点击保存按钮完成修改');
}

// 提交基本资料表单
const submitBasicForm = async () => {
  if (!basicFormRef.value) return
  
  try {
    await basicFormRef.value.validate()
    
    // 这里应该调用API保存用户信息
    // 模拟API调用
    setTimeout(() => {
      // 更新本地用户信息
      userInfo.name = basicForm.name
      userInfo.email = basicForm.email
      userInfo.phone = basicForm.phone
      userInfo.avatar = basicForm.avatar
      
      ElMessage.success('个人信息更新成功')
    }, 500)
  } catch (error) {
    console.error('表单验证失败', error)
  }
}

// 重置基本资料表单
const resetBasicForm = () => {
  if (basicFormRef.value) {
    basicFormRef.value.resetFields()
  }
}

// 密码表单
const passwordFormRef = ref()
const passwordForm = reactive({
  currentPassword: '',
  newPassword: '',
  confirmPassword: ''
})

// 密码表单验证规则
const passwordRules = {
  currentPassword: [
    { required: true, message: '请输入当前密码', trigger: 'blur' },
    { min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur' }
  ],
  newPassword: [
    { required: true, message: '请输入新密码', trigger: 'blur' },
    { min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur' }
  ],
  confirmPassword: [
    { required: true, message: '请再次输入新密码', trigger: 'blur' },
    {
      validator: (rule: any, value: string, callback: any) => {
        if (value !== passwordForm.newPassword) {
          callback(new Error('两次输入密码不一致'))
        } else {
          callback()
        }
      },
      trigger: 'blur'
    }
  ]
}

// 提交密码表单
const submitPasswordForm = async () => {
  if (!passwordFormRef.value) return
  
  try {
    await passwordFormRef.value.validate()
    
    // 这里应该调用API修改密码
    // 模拟API调用
    setTimeout(() => {
      ElMessage.success('密码修改成功')
      resetPasswordForm()
    }, 500)
  } catch (error) {
    console.error('表单验证失败', error)
  }
}

// 重置密码表单
const resetPasswordForm = () => {
  if (passwordFormRef.value) {
    passwordFormRef.value.resetFields()
  }
}
</script>

<style scoped>
.profile-container {
  padding: var(--content-padding);
}

.profile-card {
  margin-bottom: 20px;
}

.profile-header {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 20px 0;
}

.avatar-container {
  margin-bottom: 16px;
}

.username {
  margin: 8px 0;
  font-size: 20px;
  color: #303133;
}

.user-role {
  color: #909399;
  font-size: 14px;
}

.profile-info {
  padding: 10px 0;
}

.info-item {
  display: flex;
  margin-bottom: 12px;
}

.info-label {
  width: 80px;
  color: #909399;
}

.info-value {
  flex: 1;
  color: #303133;
}

.settings-card {
  margin-bottom: 20px;
}

.card-header {
  padding: 0;
}

:deep(.el-tabs__header) {
  margin-bottom: 0;
}

:deep(.el-tabs__nav-wrap) {
  padding: 0 20px;
}

:deep(.el-tabs__item) {
  display: flex;
  align-items: center;
}

:deep(.el-tabs__item .el-icon) {
  margin-right: 5px;
}

.avatar-upload-container {
  display: flex;
  align-items: center;
}

.avatar-uploader {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 120px;
  height: 120px;
  border-radius: 50%;
  overflow: hidden;
  position: relative;
  cursor: pointer;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.avatar-preview {
  width: 100%;
  height: 100%;
  position: relative;
  background-color: #f5f7fa;
  display: flex;
  justify-content: center;
  align-items: center;
  overflow: hidden;
}

.avatar-hover-mask {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.5);
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  border-radius: 50%;
  color: white;
  opacity: 0;
  transition: opacity 0.3s;
}

.avatar-preview:hover .avatar-hover-mask {
  opacity: 1;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #1a73e8;
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100%;
  height: 100%;
  background-color: #f5f7fa;
  transition: all 0.3s ease;
}

.avatar-uploader-icon:hover {
  background-color: #e6f2ff;
}

.avatar-uploader-icon .el-icon {
  background-color: rgba(26, 115, 232, 0.1);
  border-radius: 50%;
  padding: 10px;
  transition: transform 0.3s ease;
}

.avatar-uploader-icon:hover .el-icon {
  transform: scale(1.1);
}

.avatar {
  width: 100%;
  height: 100%;
  display: block;
  object-fit: cover;
}

.avatar-tips {
  margin-left: 20px;
}

.tip-title {
  font-weight: bold;
  margin-bottom: 5px;
  color: var(--text-primary);
}

.tip-desc {
  font-size: 12px;
  color: var(--text-secondary);
}

.avatar-tip {
  display: none;
}
</style> 